$(function () {

    var stuTk_datagrid =  $("#stuTk_datagrid");
    var stuTk_dialog = $("#stuTk_dialog");
    
    //给 a链接统一绑定事件
    $("a[data-cmd]").click(function(){
        var methodName = $(this).data("cmd");
        //调用方法
        methodObj[methodName]();
    });

    stuTk_datagrid.datagrid({
        url: '/studentTracking/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'potentialStudent', title: '客户 ', width: 80,formatter:function (value) {
                    return value?value.name:"";
                }},
            {field: 'consultant', title: '咨询人员', width: 80,formatter:function (value) {
                    return value?value.username:"";
                }},
            {field: 'followupTime', title: '本次跟进时间', width: 80},
            {field: 'nextFollowTime', title: '下次跟进时间 ', width: 80},
            {field: 'tracePurpose', title: '跟进目的 ', width: 80,formatter:function (value) {
                    return value?value.name:"";
                }},
            {field: 'communicationMethod', title: '咨询方式 ', width: 80,formatter:function (value) {
                    return value?value.name:"";
                }},
            {field: 'qqNum', title: 'qq ', width: 80},
            {field: 'tel', title: '电话', width: 80},
            {field: 'wantedLevel', title: '意向程度 ', width: 80,formatter:function (value) {
                    return value?value.name:"";
                }},
            {field: 'auditExplain', title: '审核说明 ', width: 80},
            {field: 'auditState', title: '审核状态 ', width: 80,formatter:function (value) {
                    return value?'<font color="red">已审核</font>':'未审核';
                }},

        ]],
        toolbar: '#stuTk_toolbar',
        singleSelect: true,
        rownumbers: true,
        pagination: true,
    });

    stuTk_dialog.dialog({
        width: 650,
        height: 400,
        closed: true,
        resizable: false,
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.save();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.cancel();
                }
            }
        ],
        //清空表单数据
        onClose: function () {
            $("#clientTraceAdd_form").form('clear');
        }
    });
    //审核弹出框
    $("#audit_dialog").dialog({
        width: 450,
        height: 300,
        closed: true,
        resizable: false,
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.auditSave();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.auditCancel();
                }
            }
        ],
        //清空表单数据
        onClose: function () {
            $("#clientTraceAdd_form").form('clear');
        }
    });

    //使用一个对象来统一管理实践的方法
    var methodObj = {

        edit: function () {
            //判断是否选中了数据
            var row = stuTk_datagrid.datagrid('getSelected');
            console.log(row);
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
            $(".easyui-textbox").textbox({
                readonly:false
            })
            $(".easyui-combobox").combobox({
                readonly:false
            })
            $(".easyui-datetimebox").datetimebox({
                readonly:false
            })
            $("input[name='focusProblem'],input[name='remark']").prop('readonly',false);
            //回显操作
            $.get("/studentTracking/selectByStudentTrackingId.do",{id:row.id},function (data) {
                console.log(data);
                data["potentialStudent.id"] = data.potentialStudent.id;
                if(data.state){
                    data["state.id"] = data.state.id;
                }
                if(data.consultant.id){
                    data["consultant.id"] = data.consultant.id;
                }
                if(data.school.id){
                    data["school.id"] = data.school.id;
                }
                if(data.grade.id){
                    data["grade.id"] = data.grade.id;
                }
                if(data.communicationMethod){
                    data["communicationMethod.id"] = data.communicationMethod.id;
                }
                if(data.wantedLevel){
                    data["wantedLevel.id"] = data.wantedLevel.id;
                }
                if(data.tracePurpose){
                    data["tracePurpose.id"] = data.tracePurpose.id;
                }
                $("#clientTraceAdd_form").form('load', data);
            })
            //选中了一条数据 回显表单数据(根据同名匹配原则 row对象中的属性名要与form中name相同才可以回显)

            //弹出表格窗口
            stuTk_dialog.dialog("open");

            //设置标题
            stuTk_dialog.dialog('setTitle', '跟踪记录编辑');
        },
        //刷新方法
        renovation:function () {
            stuTk_datagrid.datagrid('reload')
        },
        //保存操作
        save: function () {
            $("#clientTraceAdd_form").form('submit', {
                url: '/studentTracking/saveOrUpdate.do',
                success: function (data) {
                    //转换为json对象
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            // load 加载和显示第一页的所有行
                            stuTk_datagrid.datagrid('reload')
                        })
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error')
                    }
                }
            })
        },

        //取消操作
        cancel: function () {
            stuTk_dialog.dialog('close');
        },
        audit:function () {
            var row = stuTk_datagrid.datagrid('getSelected');
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
            //判断是否选中了数据
            if (row.auditState) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "以审核的不能再审核", "warning");
                return;
            }
            console.log(row);
            if(row.consultant){
                row["consultant.username"] = row.consultant.username;
            }
            if(row.communicationMethod){
                row["communicationMethod.name"] = row.communicationMethod.name;
            }
            row["potentialStudent.name"] = row.potentialStudent.name;
            $("#audit_form").form('load', row);
            $("#audit_dialog").dialog('open');
            $("#audit_dialog").dialog('setTitle', '审核业务');

        },
        //审核保存方式
        auditSave:function () {
            var row = stuTk_datagrid.datagrid('getSelected');
            $("#audit_form").form('submit', {
                url: '/studentTracking/audit.do',
                queryParams:{id:row.id},
                success: function (data) {
                    //转换为json对象
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '审核成功!', 'info', function () {
                            //关闭弹出框
                            $("#audit_dialog").dialog('close');
                            // load 加载和显示第一页的所有行
                            stuTk_datagrid.datagrid('reload')
                        })
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error')
                    }
                }
            })
        },
        //审核关闭弹出框
        auditCancel:function () {
            $("#audit_dialog").dialog('close');
        }
    }
    //查看回显操作
    $("#stuTk_look").linkbutton({
        onClick:function () {
            //判断是否选中了数据
            var row = stuTk_datagrid.datagrid('getSelected');
            console.log(row);
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
            $(".easyui-textbox").textbox({
                readonly:true
            })
            $(".easyui-combobox").combobox({
                readonly:true
            })
            $(".easyui-datetimebox").datetimebox({
                readonly:true
            })
            $("input[name='focusProblem'],input[name='remark']").prop('readonly',true);
            //回显操作
            $.get("/studentTracking/selectByStudentTrackingId.do",{id:row.id},function (data) {
                data["potentialStudent.id"] = data.potentialStudent.id;
                if(data.state){
                    data["state.id"] = data.state.id;
                }
                if(data.consultant.id){
                    data["consultant.id"] = data.consultant.id;
                }
                if(data.school.id){
                    data["school.id"] = data.school.id;
                }
                if(data.grade.id){
                    data["grade.id"] = data.grade.id;
                }
                if(data.communicationMethod){
                    data["communicationMethod.id"] = data.communicationMethod.id;
                }
                if(data.wantedLevel){
                    data["wantedLevel.id"] = data.wantedLevel.id;
                }
                if(data.tracePurpose){
                    data["tracePurpose.id"] = data.tracePurpose.id;
                }
                $("#clientTraceAdd_form").form('load', data);
            })
            //选中了一条数据 回显表单数据(根据同名匹配原则 row对象中的属性名要与form中name相同才可以回显)

            //弹出表格窗口
            stuTk_dialog.dialog("open");

            //设置标题
            stuTk_dialog.dialog('setTitle', '跟踪记录编辑');
        }
    })
});

